﻿using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.ObjectModel;

namespace SimpleToDoList.ViewModels;

public partial class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        Copyright = $"Copyright \u00a9 {DateTime.Now.Year} Azrng v1.0.2";
        if (Design.IsDesignMode)
        {
            TodoItems = new ObservableCollection<ToDoItemViewModel>(new[]
                                                                    {
                                                                        new ToDoItemViewModel { Content = "Hello" },
                                                                        new ToDoItemViewModel { Content = "Avalonia", IsChecked = true }
                                                                    });
        }
    }

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(AddItemCommand))]
    private string? _newItemContent;

    /// <summary>
    /// 版权信息
    /// </summary>
    [ObservableProperty]
    private string? _copyright;


    /// <summary>
    /// 任务集合
    /// </summary>
    public ObservableCollection<ToDoItemViewModel> TodoItems { get; } = new ObservableCollection<ToDoItemViewModel>();

    /// <summary>
    /// 如果可以添加新的Item，则返回。我们需要有新的文本
    /// </summary>
    /// <returns></returns>
    private bool CanAddItem() => !string.IsNullOrWhiteSpace(NewItemContent);

    [RelayCommand(CanExecute = nameof(CanAddItem))]
    private void AddItem()
    {
        // 添加新项到列表
        TodoItems.Add(new ToDoItemViewModel { Content = NewItemContent });

        // 刷新新的项
        NewItemContent = null;
    }

    /// <summary>
    /// 移出任务
    /// </summary>
    /// <param name="item"></param>
    [RelayCommand]
    private void RemoveItem(ToDoItemViewModel item)
    {
        TodoItems.Remove(item);
    }
}